package com.koron.sinoprobe.web.engineering.service.impl;


import com.koron.sinoprobe.web.engineering.bean.po.SpAppceptProject;
import com.koron.sinoprobe.web.engineering.mapper.SpAppceptProjectMapper;
import com.koron.sinoprobe.web.engineering.service.SpAppceptProjectService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目验收-关联工程(SpAppceptProject)表服务实现类
 *
 * @author jzp
 * @since 2021-12-02 08:49:09
 */
@Service
public class SpAppceptProjectServiceImpl implements SpAppceptProjectService {

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public SpAppceptProject getById(SessionFactory factory, String id) {
        SpAppceptProjectMapper mapper = factory.getMapper(SpAppceptProjectMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<SpAppceptProject> list(SessionFactory factory, SpAppceptProject spAppceptProject) {
        SpAppceptProjectMapper mapper = factory.getMapper(SpAppceptProjectMapper.class);
        return mapper.selectList(spAppceptProject);
    }

    @Override
    @TaskAnnotation("listByAppceptId")
    public List<SpAppceptProject> listByAppceptId(SessionFactory factory, String appceptId) {
        SpAppceptProject spAppceptProject = new SpAppceptProject();
        spAppceptProject.setAppceptId(appceptId);
        return list(factory, spAppceptProject);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, SpAppceptProject spAppceptProject) {
        SpAppceptProjectMapper mapper = factory.getMapper(SpAppceptProjectMapper.class);
        return mapper.insertOrUpdate(spAppceptProject) > 0;
    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<SpAppceptProject> lists) {
        if (CollectionUtils.isEmpty(lists)) {
            return false;
        }
        SpAppceptProjectMapper mapper = factory.getMapper(SpAppceptProjectMapper.class);
        return mapper.insertOrUpdateBatch(lists) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, SpAppceptProject spAppceptProject) {
        SpAppceptProjectMapper mapper = factory.getMapper(SpAppceptProjectMapper.class);
        return mapper.update(spAppceptProject) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteBatchIds")
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        SpAppceptProjectMapper mapper = factory.getMapper(SpAppceptProjectMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @TaskAnnotation("removeByDivisionId")
    public boolean removeByDivisionId(SessionFactory factory, String appceptId) {
        if (StringUtils.isEmpty(appceptId)) {
            return false;
        }
        List<SpAppceptProject> spAppceptProjects = listByAppceptId(factory, appceptId);
        return deleteBatchIds(factory, spAppceptProjects.stream().map(SpAppceptProject::getId).collect(Collectors.toList()));

    }
}
